package com.wgchao.amc.mapstruct.mapper.expand;

import com.wgchao.amc.entity.base.SysBaseAgency;
import com.wgchao.amc.entity.base.SysBaseUser;
import com.wgchao.amc.mapstruct.DTOMapper;
import com.wgchao.amc.mapstruct.dto.SysBaseAgencyDTO;
import com.wgchao.amc.mapstruct.mapper.SysBaseAgencyMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseUserMapper;
import com.wgchao.amc.repository.base.SysBaseAgencyRepository;
import com.wgchao.amc.repository.base.SysBaseUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: SysBaseAgencyMapperExpand
 * @author: wgchao
 * @createTime: 2019/10/20 1:34 PM
 * @Description: TODO
 */
@Component
public class SysBaseAgencyMapperExpand implements DTOMapper<SysBaseAgency, SysBaseAgencyDTO>, CommonExpand<SysBaseAgencyDTO>  {
    @Autowired
    private SysBaseAgencyMapper agencyMapper;
    @Autowired
    private SysBaseAgencyRepository agencyRepository;
    @Autowired
    private SysBaseUserMapper userMapper;
    @Autowired
    private SysBaseUserRepository userRepository;

    @Override
    public SysBaseAgency toEntity(SysBaseAgencyDTO agencyDTO) {
        return agencyMapper.toEntity(agencyDTO);
    }

    @Override
    public SysBaseAgencyDTO toDTO(SysBaseAgency sysBaseAgency) {
        return agencyMapper.toDTO(sysBaseAgency);
    }

    @Override
    public List<SysBaseAgency> toEntity(List<SysBaseAgencyDTO> sysBaseAgencyDTOS) {
        return agencyMapper.toEntity(sysBaseAgencyDTOS);
    }

    @Override
    public List<SysBaseAgencyDTO> toDTO(List<SysBaseAgency> sysBaseAgencies) {
        return agencyMapper.toDTO(sysBaseAgencies);
    }

    @Override
    public SysBaseAgencyDTO getCURDUserInfo(SysBaseAgencyDTO agencyDTO) {
        if(agencyDTO.getCreateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(agencyDTO.getCreateUser());
            if(userOptional.isPresent()){
                agencyDTO.setCreateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(agencyDTO.getLastUpdateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(agencyDTO.getLastUpdateUser());
            if(userOptional.isPresent()){
                agencyDTO.setLastUpdateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(agencyDTO.getDelUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(agencyDTO.getDelUser());
            if(userOptional.isPresent()){
                agencyDTO.setDelUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        return agencyDTO;
    }


    public SysBaseAgencyDTO getParent(SysBaseAgencyDTO agencyDTO){
        if(!StringUtils.isEmpty(agencyDTO.getParCode())){
            Optional<SysBaseAgency> agencyOptional = agencyRepository.findById(agencyDTO.getParCode());
            if(agencyOptional.isPresent()){
                agencyDTO.setParAgencyInfo(toDTO(agencyOptional.get()));
            }
        }
        return agencyDTO;
    }
}
